package com.example.mymvvmdemo.models.launch;

import android.content.Intent;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

import com.example.mymvvmdemo.R;
import com.example.mymvvmdemo.constants.UrlConstants;
import com.example.mymvvmdemo.constants.UrlTypeConstants;
import com.example.mymvvmdemo.entity.FailEntity;
import com.example.mymvvmdemo.models.agreement.AgreementActivity;
import com.example.mymvvmdemo.models.login.LoginActivity;
import com.example.mymvvmdemo.models.login.entity.LoginEntity;
import com.example.mymvvmdemo.models.main.MainActivity;
import com.example.mymvvmdemo.MyApplication;
import com.example.mymvvmdemo.constants.Constants;
import com.example.mymvvmdemo.net.presenter.PostBodyPresenter;
import com.example.mymvvmdemo.net.view.IView;
import com.example.mymvvmdemo.utils.LogUtils;
import com.example.mymvvmdemo.utils.SpUtils;
import com.example.mymvvmdemo.utils.StrUtils;
import com.example.mymvvmdemo.utils.ToastUtils;
import com.google.gson.Gson;

import java.util.HashMap;


public class LaunchActivity extends AppCompatActivity {


    private String authToken;
    private boolean isPrivate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        authToken = SpUtils.getSpUtils(MyApplication.getInstance(), Constants.ACCOUNT_NAME)
                .getStringValue(Constants.SP_AUTH_TOKEN, null);
        isPrivate = SpUtils.getSpUtils(MyApplication.getInstance(), Constants.ACCOUNT_NAME)
                .getBooleanValue(Constants.USER_PRIVATE, false);

        startActivity();
    }

    private void startActivity() {

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        LogUtils.setIsPrivate(true);
        Intent intent = new Intent();

        //正常使用后打开
        if (!isPrivate) {
            intent.setClass(LaunchActivity.this, AgreementActivity.class);
        } else if (!StrUtils.isEmpty(authToken)) {
            //已登录
//            intent.setClass(LaunchActivity.this, MainActivity.class);
            startLoadToken();
            return;
        } else {
            //未登录状态
            intent.setClass(LaunchActivity.this, LoginActivity.class);
        }
        startActivity(intent);
        finish();

    }

    private void startLoadToken() {

        String account = SpUtils.getSpUtils(MyApplication.getInstance(), Constants.ACCOUNT_NAME)
                .getStringValue(Constants.USER_ACCOUNT, null);
        String password = SpUtils.getSpUtils(MyApplication.getInstance(), Constants.ACCOUNT_NAME)
                .getStringValue(Constants.USER_PASSWORD, null);
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("account", account);
        hashMap.put("password", password);
        postBodyPresenter.startLoadPostBody(UrlConstants.login, hashMap, UrlTypeConstants.login);

    }

    public void startMainActivity() {
        Intent intent = new Intent();
        intent.setClass(LaunchActivity.this, MainActivity.class);
        startActivity(intent);
        finish();
    }

    private PostBodyPresenter postBodyPresenter = new PostBodyPresenter(new IView.PostBodyView() {
        @Override
        public void loadPostBodySuccess(String result, String type, int code) {
            try {
                Gson gson = new Gson();
                LoginEntity loginEntity = gson.fromJson(result, LoginEntity.class);
                String token = loginEntity.getData().getToken().getAccess();
                LogUtils.d("login token:" + token);
                MyApplication.setAuthToken(token);

            } catch (Exception e) {
            }
            startMainActivity();

        }

        @Override
        public void loadPostBodyFail(String message, String type) {
            startMainActivity();
        }

        @Override
        public void loadPostBodyAccountError(int code) {
            startMainActivity();
        }

        @Override
        public void loadPostBodyNetWorkError(String type) {
            startMainActivity();
        }
    });

}
